HTTP Methods এর জন্য Response Entity হ্যান্ডল করা

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) HTTP Methods এবং Request Handling |
113
113

Spring Boot-এ ResponseEntity একটি গুরুত্বপূর্ণ ক্লাস যা HTTP রেসপন্সের স্ট্যাটাস কোড, হেডার, এবং বডি পরিচালনা করতে ব্যবহৃত হয়। HTTP Methods এর মাধ্যমে ResponseEntity হ্যান্ডল করার পদ্ধতি নিম্নে আলোচনা করা হলো:


ResponseEntity ব্যবহার কেন জরুরি?

  1. HTTP স্ট্যাটাস কোড প্রদান করা: ক্লায়েন্টকে সঠিক HTTP স্ট্যাটাস কোড পাঠানো।
  2. হেডার সেট করা: রেসপন্সের জন্য কাস্টম HTTP হেডার সংযুক্ত করা।
  3. বডি প্রদান করা: রেসপন্স বডি পাঠানো।
  4. এলাস্টিক রেসপন্স: স্ট্যাটাস, হেডার, এবং বডি একসাথে হ্যান্ডল করা।

১. ResponseEntity হ্যান্ডল করা: GET Method

উদাহরণ:

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public ResponseEntity<String> getDataFromApi() {
        String url = "https://jsonplaceholder.typicode.com/posts";
        
        // API থেকে ResponseEntity গ্রহণ করা
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

        return response;
    }
}

Controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @Autowired
    private ApiService apiService;

    @GetMapping("/getPosts")
    public ResponseEntity<String> getPosts() {
        return apiService.getDataFromApi();
    }
}

২. ResponseEntity হ্যান্ডল করা: POST Method

উদাহরণ:

import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public ResponseEntity<String> postDataToApi(String jsonPayload) {
        String url = "https://jsonplaceholder.typicode.com/posts";

        HttpEntity<String> request = new HttpEntity<>(jsonPayload);
        
        // POST Method-এর মাধ্যমে ডেটা পাঠানো
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

        return response;
    }
}

Controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @Autowired
    private ApiService apiService;

    @PostMapping("/createPost")
    public ResponseEntity<String> createPost(@RequestBody String jsonPayload) {
        return apiService.postDataToApi(jsonPayload);
    }
}

৩. ResponseEntity হ্যান্ডল করা: PUT Method

উদাহরণ:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public ResponseEntity<String> updateDataInApi(String jsonPayload, int id) {
        String url = "https://jsonplaceholder.typicode.com/posts/" + id;

        HttpEntity<String> request = new HttpEntity<>(jsonPayload);
        
        // PUT Method ব্যবহার
        restTemplate.put(url, request);

        return new ResponseEntity<>("Resource updated successfully", HttpStatus.OK);
    }
}

Controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @Autowired
    private ApiService apiService;

    @PutMapping("/updatePost")
    public ResponseEntity<String> updatePost(@RequestBody String jsonPayload, @RequestParam int id) {
        return apiService.updateDataInApi(jsonPayload, id);
    }
}

৪. ResponseEntity হ্যান্ডল করা: DELETE Method

উদাহরণ:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public ResponseEntity<String> deleteDataFromApi(int id) {
        String url = "https://jsonplaceholder.typicode.com/posts/" + id;

        // DELETE Method ব্যবহার
        restTemplate.delete(url);

        return new ResponseEntity<>("Resource deleted successfully", HttpStatus.OK);
    }
}

Controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @Autowired
    private ApiService apiService;

    @DeleteMapping("/deletePost")
    public ResponseEntity<String> deletePost(@RequestParam int id) {
        return apiService.deleteDataFromApi(id);
    }
}

ResponseEntity-এর গুরুত্বপূর্ণ বৈশিষ্ট্য

  1. Custom HTTP Status Code:

    return new ResponseEntity<>("Error occurred", HttpStatus.BAD_REQUEST);
    
  2. Custom HTTP Headers:

    HttpHeaders headers = new HttpHeaders();
    headers.set("Custom-Header", "HeaderValue");
    return new ResponseEntity<>("Response Body", headers, HttpStatus.OK);
    
  3. ResponseEntity.ok() Shortcut:

    return ResponseEntity.ok("Response Body");
    
  4. ResponseEntity.noContent() for DELETE:

    return ResponseEntity.noContent().build();
    

উপসংহার

Spring Boot-এ ResponseEntity ব্যবহার করে HTTP Methods এর জন্য রেসপন্স বডি, হেডার, এবং স্ট্যাটাস কোড সহজেই হ্যান্ডল করা যায়। এটি অ্যাপ্লিকেশনকে আরো লচিৎ এবং কাস্টমাইজড রেসপন্স প্রদান করতে সহায়তা করে।

যদি আরো বিস্তারিত সাহায্য প্রয়োজন হয়, জানান! 😊

Content added By
টপ রেটেড অ্যাপ

স্যাট অ্যাকাডেমী অ্যাপ

আমাদের অল-ইন-ওয়ান মোবাইল অ্যাপের মাধ্যমে সীমাহীন শেখার সুযোগ উপভোগ করুন।

ভিডিও
লাইভ ক্লাস
এক্সাম
ডাউনলোড করুন
Promotion